using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class AccountSubject
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("AccountSubjectID", "HLJXC_AccountSubject");
		}
		public bool Exists(int AccountSubjectID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HLJXC_AccountSubject");
			stringBuilder.Append(" where AccountSubjectID=@AccountSubjectID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountSubjectID", SqlDbType.Int, 4)
			};
			array[0].Value = AccountSubjectID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.AccountSubject model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HLJXC_AccountSubject(");
			stringBuilder.Append("AccountSParentID,AccountSRootID,AccountSDepth,AccountSNumber,AccountSName,AccountSSpell,AccountSLend,AccountSAuxiliaryCheck,Remark)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@AccountSParentID,@AccountSRootID,@AccountSDepth,@AccountSNumber,@AccountSName,@AccountSSpell,@AccountSLend,@AccountSAuxiliaryCheck,@Remark)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountSParentID", SqlDbType.Int, 4),
				new SqlParameter("@AccountSRootID", SqlDbType.Int, 4),
				new SqlParameter("@AccountSDepth", SqlDbType.Int, 4),
				new SqlParameter("@AccountSNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@AccountSName", SqlDbType.NVarChar, 50),
				new SqlParameter("@AccountSSpell", SqlDbType.NVarChar, 50),
				new SqlParameter("@AccountSLend", SqlDbType.NVarChar, 50),
				new SqlParameter("@AccountSAuxiliaryCheck", SqlDbType.NVarChar, 50),
				new SqlParameter("@Remark", SqlDbType.NVarChar, 255)
			};
			array[0].Value = model.AccountSParentID;
			array[1].Value = model.AccountSRootID;
			array[2].Value = model.AccountSDepth;
			array[3].Value = model.AccountSNumber;
			array[4].Value = model.AccountSName;
			array[5].Value = model.AccountSSpell;
			array[6].Value = model.AccountSLend;
			array[7].Value = model.AccountSAuxiliaryCheck;
			array[8].Value = model.Remark;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.AccountSubject model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HLJXC_AccountSubject set ");
			stringBuilder.Append("AccountSParentID=@AccountSParentID,");
			stringBuilder.Append("AccountSRootID=@AccountSRootID,");
			stringBuilder.Append("AccountSDepth=@AccountSDepth,");
			stringBuilder.Append("AccountSNumber=@AccountSNumber,");
			stringBuilder.Append("AccountSName=@AccountSName,");
			stringBuilder.Append("AccountSSpell=@AccountSSpell,");
			stringBuilder.Append("AccountSLend=@AccountSLend,");
			stringBuilder.Append("AccountSAuxiliaryCheck=@AccountSAuxiliaryCheck,");
			stringBuilder.Append("Remark=@Remark");
			stringBuilder.Append(" where AccountSubjectID=@AccountSubjectID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountSParentID", SqlDbType.Int, 4),
				new SqlParameter("@AccountSRootID", SqlDbType.Int, 4),
				new SqlParameter("@AccountSDepth", SqlDbType.Int, 4),
				new SqlParameter("@AccountSNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@AccountSName", SqlDbType.NVarChar, 50),
				new SqlParameter("@AccountSSpell", SqlDbType.NVarChar, 50),
				new SqlParameter("@AccountSLend", SqlDbType.NVarChar, 50),
				new SqlParameter("@AccountSAuxiliaryCheck", SqlDbType.NVarChar, 50),
				new SqlParameter("@Remark", SqlDbType.NVarChar, 255),
				new SqlParameter("@AccountSubjectID", SqlDbType.Int, 4)
			};
			array[0].Value = model.AccountSParentID;
			array[1].Value = model.AccountSRootID;
			array[2].Value = model.AccountSDepth;
			array[3].Value = model.AccountSNumber;
			array[4].Value = model.AccountSName;
			array[5].Value = model.AccountSSpell;
			array[6].Value = model.AccountSLend;
			array[7].Value = model.AccountSAuxiliaryCheck;
			array[8].Value = model.Remark;
			array[9].Value = model.AccountSubjectID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int AccountSubjectID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HLJXC_AccountSubject ");
			stringBuilder.Append(" where AccountSubjectID=@AccountSubjectID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountSubjectID", SqlDbType.Int, 4)
			};
			array[0].Value = AccountSubjectID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string AccountSubjectIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HLJXC_AccountSubject ");
			stringBuilder.Append(" where AccountSubjectID in (" + AccountSubjectIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.AccountSubject GetModel(int AccountSubjectID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 * from HLJXC_AccountSubject ");
			stringBuilder.Append(" where AccountSubjectID=@AccountSubjectID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountSubjectID", SqlDbType.Int, 4)
			};
			array[0].Value = AccountSubjectID;
			HKB.Data.Model.AccountSubject accountSubject = new HKB.Data.Model.AccountSubject();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.AccountSubject result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.AccountSubject DataRowToModel(DataRow row)
		{
			HKB.Data.Model.AccountSubject accountSubject = new HKB.Data.Model.AccountSubject();
			if (row != null)
			{
				if (row["AccountSubjectID"] != null && row["AccountSubjectID"].ToString() != "")
				{
					accountSubject.AccountSubjectID = int.Parse(row["AccountSubjectID"].ToString());
				}
				if (row["AccountSParentID"] != null && row["AccountSParentID"].ToString() != "")
				{
					accountSubject.AccountSParentID = new int?(int.Parse(row["AccountSParentID"].ToString()));
				}
				if (row["AccountSRootID"] != null && row["AccountSRootID"].ToString() != "")
				{
					accountSubject.AccountSRootID = new int?(int.Parse(row["AccountSRootID"].ToString()));
				}
				if (row["AccountSDepth"] != null && row["AccountSDepth"].ToString() != "")
				{
					accountSubject.AccountSDepth = new int?(int.Parse(row["AccountSDepth"].ToString()));
				}
				if (row["AccountSNumber"] != null)
				{
					accountSubject.AccountSNumber = row["AccountSNumber"].ToString();
				}
				if (row["AccountSName"] != null)
				{
					accountSubject.AccountSName = row["AccountSName"].ToString();
				}
				if (row["AccountSSpell"] != null)
				{
					accountSubject.AccountSSpell = row["AccountSSpell"].ToString();
				}
				if (row["AccountSLend"] != null)
				{
					accountSubject.AccountSLend = row["AccountSLend"].ToString();
				}
				if (row["AccountSAuxiliaryCheck"] != null)
				{
					accountSubject.AccountSAuxiliaryCheck = row["AccountSAuxiliaryCheck"].ToString();
				}
				if (row["Remark"] != null)
				{
					accountSubject.Remark = row["Remark"].ToString();
				}
			}
			return accountSubject;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * FROM HLJXC_AccountSubject ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * FROM HLJXC_AccountSubject ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HLJXC_AccountSubject ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.AccountSubjectID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HLJXC_AccountSubject T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public HKB.Data.Model.AccountSubject GetModelByName(string accountSubjectName)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 * from HLJXC_AccountSubject ");
			stringBuilder.Append(" where AccountSName=@AccountSName");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountSName", SqlDbType.NVarChar, 50)
			};
			array[0].Value = accountSubjectName;
			HKB.Data.Model.AccountSubject accountSubject = new HKB.Data.Model.AccountSubject();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.AccountSubject result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public DataSet GetSubjcetNameBySubjectID(int subjcetID)
		{
			string commandText = "select dbo.GetAccountSubjectPathName(AccountSubjectID) as AccountSName from HLJXC_AccountSubject where AccountSubjectID=" + subjcetID;
			return Conn.Query(commandText, new IDbDataParameter[0]);
		}
	}
}
